﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.App_Codes
{
    public class GlobalIndexRequest
    {
        /// <summary>
        /// 获取全球股指信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetGlobalIndexFromWeb()
        {
            object obj = CacheManager.GetCache("GlobalIndexIn_Web");
            if (obj == null)
            {
                var list = GetGlobalIndex();
                CacheManager.AddCache("GlobalIndexIn_Web", list, App_Codes.GetCount.CacheTime);
                return list;
            }
            else
            {
                return obj as string;
            }
        }


        /// <summary>
        /// 获取绵白糖城市信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static string GetGlobalIndex()
        {
            List<TabInfo> tabInfos = new List<TabInfo>();
            F10ParamEntity entity = new F10ParamEntity(2);
            entity.Filters = new List<string>();
            entity.TableName = "FEED_FEX_QUOTESNAP";
            entity.Prefix = "Quote";
            entity.Filters.Add("JYS-str=指数");
            entity.Filters.Add("CODE-in-str=DQII,SP5I,NSDI,FTLI,JPII,HKII,DAXI");
            entity.Limit = 100;

            Newtonsoft.Json.Linq.JToken token;



            int totalCount;


            bool result = F10Query.TryParse(entity, false, out token, out totalCount);

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (result)
            {
                for (int i = 0; i < token.Count(); i++)
                {
                    App_Codes.GlobalIndex info = new App_Codes.GlobalIndex();
                    double t = 0;
                    info.Name = token[i].J_String("NAME");
                    info.New = token[i].J_Double("NEW", t);
                    info.PXCHG = token[i].J_Double("PXCHG", t);
                    //<li><strong>道琼斯</strong> 13235.39 <span class="greenUp">100.38</span></li>

                    sb.Append("<li><strong>" + info.Name + "</strong> " + info.New + " <span class=\"" + GetRaiseClass(info.PXCHG) + "\">" + info.PXCHG + "</span></li>");
                }
            }
            return sb.ToString();
        }

        public static string GetRaiseClass(double value)
        {
            return value == 0 ? "" : (value > 0 ? "greenUp" : "redDown");
        }
    }
}